package com.it.schoolbookshop_back.service.impl;

import com.it.schoolbookshop_back.service.AdminBookManageService;
import com.it.schoolbookshop_back.mapper.AdminBookManageMapper;
import com.it.schoolbookshop_back.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class AdminBookManageServiceImpl implements AdminBookManageService {
    
    @Autowired
    private AdminBookManageMapper adminBookManageMapper;
    
    @Autowired
    private BookMapper bookMapper;
    
    @Override
    @Transactional
    public boolean deleteBook(Integer bookId) {
        // 验证参数
        if (bookId == null) {
            throw new IllegalArgumentException("书籍ID不能为空");
        }
        
        // 检查书籍是否存在
        if (bookMapper.getBookById(bookId) == null) {
            throw new IllegalArgumentException("书籍不存在");
        }
        
        // 执行删除操作
        int result = adminBookManageMapper.deleteBook(bookId);
        
        // 记录删除操作
        log.info("管理员删除书籍 ID: {}, 结果: {}", bookId, result > 0 ? "成功" : "失败");
        
        return result > 0;
    }
}
